define(['jquery', 'template'], ($, template) => {
  class CartEvent {
    constructor () {
      this.renderCart()
      this.selectAllEvent()
    }

    selectAllEvent () {
      const _this = this
      $('.product-select-all input').on('click', function () {
        _this.selectAll(this)
      })
    }

    selectEvent () {
      const _this = this
      $('.product-select input').on('click', function () {
        _this.changeSelected(this)
        _this.switchCss(this)
      })
    }

    addEvent () {
      const _this = this
      $('.p-stock-btn .addNum').on('click', function () {
        _this.numChange(this, '+')
      })
    }

    minEvent () {
      const _this = this
      $('.p-stock-btn .minNum').on('click', function () {
        const $item = $(this).parents('.sc-list')
        const value = $item.find('.p-stock-text').val()
        if (value <= 1) {
          $(this).addClass('disabled')
          return false
        }
        _this.numChange(this, '-')
      })
    }

    delEvent () {
      const _this = this
      $('.p-del').on('click', function () {
        if (confirm('确定要删除这件商品？')) {
          const index = $(this).parents('.sc-list').data('index')

          let cart = JSON.parse(localStorage.getItem('cart'))

          cart.splice(index, 1)
          localStorage.setItem('cart', JSON.stringify(cart))

          _this.renderCart()
        }
      })

      $('.del-all').on('click', ()=>{
        if(confirm('确定要清空购物车么？')) {
          localStorage.setItem('cart','')
          this.renderCart()
        }
      })
    }

    valueEvent () {
      const _this = this
      $('.p-stock-text').on('change', function () {
        let value = $(this).val()
        if (value <= 1) {
          $(this).val(1)
        }

        _this.numChange(this, null)
      })
    }

    switchCss (checkbox) {
      const $checkbox = $(checkbox)
      if ($checkbox.hasClass('checked')) {
        $checkbox.removeClass('checked')
      } else {
        $checkbox.addClass('checked')
      }
    }

    changeSelected (checkbox) {
      const $checkbox = $(checkbox)
      const index = $checkbox.parents('.sc-list').data('index')
      let cart = JSON.parse(localStorage.getItem('cart'))

      // 修改商品状态
      cart[index].selected = !$checkbox.hasClass('checked')

      localStorage.setItem('cart', JSON.stringify(cart))

      // 重新计算总价
      this.calcTotal()
    }

    numChange (btn, cmd) {
      const $item = $(btn).parents('.sc-list')
      const $value = $item.find('.p-stock-text')

      const index = $item.data('index')

      let value = parseInt($value.val())
      if (cmd === '+') {
        value++
      } else if (cmd === '-') {
        value--
      }

      $value.val(value)

      let cart = JSON.parse(localStorage.getItem('cart'))

      cart[index].num = value

      localStorage.setItem('cart', JSON.stringify(cart))

      this.renderCart()
    }

    renderCart () {

      if (!localStorage.getItem('cart')) {
        $('.sc-empty').show()
        $('.sc-has').hide()
        return
      }
      $('.sc-empty').hide()
      $('.sc-has').show()
      let cart = JSON.parse(localStorage.getItem('cart'))

      // 渲染商品部分
      $('.cart-list').html(template('shopTemplate', { cart }))

      this.selectEvent()
      this.calcTotal()
      this.addEvent()
      this.minEvent()
      this.valueEvent()
      this.delEvent()
    }

    calcTotal () {
      let cart = JSON.parse(localStorage.getItem('cart'))

      let total = cart.reduce((prev, item) => {
        if (item.selected) {
          prev.price += item.originPrice * item.num
          if (item.sale) prev.sale += item.sale * item.num
          prev.num += item.num
        }
        return prev
      }, { price: 0, sale: 0, num: 0 })

      let selectAll = cart.every(item => {
        return item.selected === true
      })

      // 修改页面
      $('.product-select-all>input').removeClass().addClass(selectAll ? 'checked' : '')
      $('.sc-tools-price').html(template('totalTemplate', total))
    }

    selectAll (checkAll) {
      let flag = !$(checkAll).hasClass('checked')

      console.log(flag)

      let cart = JSON.parse(localStorage.getItem('cart'))
      cart = cart.map(item => {
        item.selected = flag
        return item
      })

      localStorage.setItem('cart', JSON.stringify(cart))
      this.renderCart()
    }

  }

  return CartEvent
})
